﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LineWatch_WinForm;
using System.Globalization;
using System.Configuration;
using System.IO;
using LineWatch.Common;
using DBAccessLib;

namespace LineWatch_WinForm
{
    public partial class Setup : Form
    {
        public Setup()
        {
            InitializeComponent();

            if (!string.IsNullOrWhiteSpace(ConfigInfo.LineCode1))
            {
                this.groupBox1.Text = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode1);
            }
            else
            {
                this.groupBox1.Enabled = false;
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.LineCode2))
            {
                this.groupBox2.Text = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode2);
            }
            else
            {
                this.groupBox2.Enabled = false;
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.LineCode3))
            {
                this.groupBox3.Text = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode3);
            }
            else
            {
                this.groupBox3.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }
            ////SerialPortOperator sp = new SerialPortOperator("COM1", 9600);//ConfigurationManager.AppSettings["ComPort"], 9600);
            ////sp.WriteLine("@00WD5000" + outV.ToString("X").PadLeft(4, '0'));// ConfigurationManager.AppSettings["PlanNumDM"].PadLeft(4, '0') + outV.ToString("X").PadLeft(4, '0'));
            ////sp.ClosePort();

            ConfigInfo.PlanNumSettingCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine1"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string text = textBox2.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }
            ////SerialPortOperator sp = new SerialPortOperator("COM1", 9600);//ConfigurationManager.AppSettings["ComPort"], 9600);
            ////sp.WriteLine("@00WD5004" + outV.ToString("X").PadLeft(4, '0'));// ConfigurationManager.AppSettings["PlanNumDM"].PadLeft(4, '0') + outV.ToString("X").PadLeft(4, '0'));
            ////sp.ClosePort();

            ConfigInfo.NowPlanSettingCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine1"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ////SerialPortOperator sp = new SerialPortOperator("COM1", 9600);//ConfigurationManager.AppSettings["ComPort"], 9600);
            ////sp.WriteLine("@00WD50000000");// + ConfigurationManager.AppSettings["PlanNumDM"].PadLeft(4, '0') + "0000");
            ////Thread.Sleep(100);
            ////sp.WriteLine("@00WD11400000");// + ConfigurationManager.AppSettings["ProductNumDM"].PadLeft(4, '0') + "0000");
            ////Thread.Sleep(100);
            ////sp.WriteLine("@00WD10860000");// + ConfigurationManager.AppSettings["StopTimeDM"].PadLeft(4, '0') + "0000");
            ////Thread.Sleep(100);
            ////sp.ClosePort();


            ConfigInfo.PlanNumResetCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine1"].ToString().Trim() + "0000";
            ConfigInfo.NowPlanResetCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine1"].ToString().Trim() + "0000";
            ConfigInfo.ProductNumResetCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["ProductNumDMPositionLine1"].ToString().Trim() + "0000";
            ConfigInfo.StopTimeResetCommandLine1 = "@00WD" + ConfigurationManager.AppSettings["StopTimeDMPositionLine1"].ToString().Trim() + "0000";

            MessageBox.Show("复位成功");
        }


        private void button4_Click(object sender, EventArgs e)
        {
            string text = textBox3.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }

            ConfigInfo.PlanNumSettingCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine2"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string text = textBox4.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }

            ConfigInfo.NowPlanSettingCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine2"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ConfigInfo.PlanNumResetCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine2"].ToString().Trim() + "0000";
            ConfigInfo.NowPlanResetCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine2"].ToString().Trim() + "0000";
            ConfigInfo.ProductNumResetCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["ProductNumDMPositionLine2"].ToString().Trim() + "0000";
            ConfigInfo.StopTimeResetCommandLine2 = "@00WD" + ConfigurationManager.AppSettings["StopTimeDMPositionLine2"].ToString().Trim() + "0000";

            MessageBox.Show("复位成功");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string text = textBox5.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }

            ConfigInfo.PlanNumSettingCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine3"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string text = textBox6.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }

            ConfigInfo.NowPlanSettingCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine3"].ToString().Trim() + outV.ToString("X").PadLeft(4, '0');
            MessageBox.Show("设置成功");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            ConfigInfo.PlanNumResetCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["PlanNumDMPositionLine3"].ToString().Trim() + "0000";
            ConfigInfo.NowPlanResetCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["NowPlanDMPositionLine3"].ToString().Trim() + "0000";
            ConfigInfo.ProductNumResetCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["ProductNumDMPositionLine3"].ToString().Trim() + "0000";
            ConfigInfo.StopTimeResetCommandLine3 = "@00WD" + ConfigurationManager.AppSettings["StopTimeDMPositionLine3"].ToString().Trim() + "0000";

            MessageBox.Show("复位成功");
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.csv)|*.csv";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    if (myStream == null)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }

            LinePositionModel model = new LinePositionModel();
            model.LinePositionTable = ConfigInfo.DtAllPositions.Clone();
            DataTable tempLinePositionTable = ConfigInfo.DtAllPositions.Clone();

            //create object of CSVReader and pass the stream

            Encoding encoding = Encoding.GetEncoding(ConfigurationManager.AppSettings["CSVEncoding"]);
            CSVReader reader = new CSVReader(myStream, encoding);

            string[] data = new string[100];
            int lineIndex = 0;

            while ((data = reader.GetCSVLine()) != null)
            {
                lineIndex++;
                if (lineIndex == 1)
                {
                    continue;
                }

                if (data.Length >= 5)
                {
                    DataRow newRow = model.LinePositionTable.NewRow();

                    newRow["Line_Code"] = data[0].Trim();
                    newRow["SPositionID"] = data[1].Trim();
                    newRow["FPositionID"] = data[2].Trim();
                    newRow["Position"] = data[3].Trim();
                    newRow["SetDate"] = string.Empty;
                    newRow["User"] = string.Empty;

                    if (!string.IsNullOrWhiteSpace(data[4].Trim()))
                    {
                        newRow["Content1"] = data[4].Trim();
                        newRow["Content2"] = "1";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 6) && !string.IsNullOrWhiteSpace(data[5].Trim()))
                    {
                        newRow["Content1"] = data[5].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "1";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 7) && !string.IsNullOrWhiteSpace(data[6].Trim()))
                    {
                        newRow["Content1"] = data[6].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "1";
                    }

                    newRow["Content5"] = string.Empty;
                    newRow["Content6"] = string.Empty;
                    model.LinePositionTable.Rows.Add(newRow);
                }
            }

            // Add Existing Positions
            foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
            {
                string lineCodeTemp = row["Line_Code"].ToString().Trim();

                DataRow[] rows = model.LinePositionTable.Select("Line_Code = '" + lineCodeTemp + "'");

                if ((rows == null) || (rows.Length <= 0))
                {
                    tempLinePositionTable.ImportRow(row);
                    tempLinePositionTable.AcceptChanges();
                }
            }

            foreach (DataRow row in tempLinePositionTable.Rows)
            {
                model.LinePositionTable.ImportRow(row);
                model.LinePositionTable.AcceptChanges();
            }


            bool result = Position.SaveAllLinePositions(model);

            if (result)
            {
                ConfigInfo.InitAllPositions();
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (myStream != null)
            {
                myStream.Close();
            }
        }
    }
}
